package com.example.forum2.services.impl;

import com.example.forum2.common.AppResult;
import com.example.forum2.common.ResultCode;
import com.example.forum2.dao.ArticleMapper;
import com.example.forum2.exception.ApplicationException;
import com.example.forum2.model.Article;
import com.example.forum2.model.Board;
import com.example.forum2.model.User;
import com.example.forum2.services.IArticleService;
import com.example.forum2.services.IBoardService;
import com.example.forum2.services.IUserService;
import com.example.forum2.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class ArticleServiceImpl implements IArticleService {
    @Autowired
    private IUserService iUserService;
    @Autowired
    private IBoardService iBoardService;

    @Autowired
    private ArticleMapper articleMapper;
//    添加帖子的操作.
    @Override
    public void create(Article article) {
//        第一步还是非空校验
        if(article==null || article.getUserId()==null
        ||article.getBoardId()==null
        || StringUtil.isEmpty(article.getTitle())
        || StringUtil.isEmpty(article.getContent())){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
//        第二步是设置文章的默认值
        article.setVisitCount(0);
        article.setReplyCount(0);
        article.setLikeCount(0);
        article.setDeleteState((byte)0);
        article.setState((byte)0);
//        时间
        Date date=new Date();
        article.setUpdateTime(date);
        article.setCreateTime(date);
//       写入数据库
        int articleRow=articleMapper.insertSelective(article);
        if(articleRow<=0){
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
        User user=iUserService.selectById(article.getUserId());
        if(user==null){
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
//        更新用户发帖数
        iUserService.addOneArticleCountById(article.getUserId());
        Board board=iBoardService.selectById(article.getBoardId());
        if(board==null){
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
//        更新板块贴子数
        iBoardService.addOneArticleCountById(article.getBoardId());
        log.info("操作成功");
    }

    @Override
    public List<Article> selectAll() {
//        这里也没有必要对参数进行返回,所以直接调用即可
//        调用dao层
        List<Article> articles=articleMapper.selectAll();
//        返回结果即可
        return articles;
    }

    @Override
    public List<Article> selectAllByBoardId(Long boardId) {
//        首先还是进行非空校验
        if(boardId==null){
//            打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
//            抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
//        检查板块是否存在
        Board board=iBoardService.selectById(boardId);
        if(board==null){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
//        所有的参数校验完成之后,就可以调用dao层的数据,返回结果
        List<Article> articles=articleMapper.selectAllByBoardId(boardId);
        return articles;
    }

    @Override
    public Article selectDetailById(Long id) {
//        首先还是非空校验
        if(id==null ||id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
//        调用dao层的方法
        Article article=articleMapper.selectDetailById(id);
//       判断结果是否为空
        if(article==null){
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));

        }
//        帖子不为空之后,需要对访问数进行更新之后返回
        Article article1=new Article();
        article1.setId(article.getId());
        article1.setVisitCount(article.getVisitCount()+1);
//        保存到数据库
        int row=articleMapper.updateByPrimaryKeySelective(article1);
        if(row!=1){
            log.warn(ResultCode.ERROR_SERVICES.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }
        article.setVisitCount(article.getVisitCount()+1);
        return article;
    }

    @Override
    public void modify(Long id, String title, String content) {
//        首先还是进行非空校验
        if(id==null||StringUtil.isEmpty(title)||StringUtil.isEmpty(content)){
//            打印日志
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
//        构建要更新的帖子对象
        Article article=new Article();
        article.setId(id);
        article.setContent(content);
        article.setTitle(title);
        article.setUpdateTime(new Date());
//        调用dao层
        int row=articleMapper.updateByPrimaryKeySelective(article);
        if(row!=1){
            log.warn("更新失败");
            throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
        }

    }

    @Override
    public Article selectById(Long id) {
//        非空校验
        if(id==null||id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article=articleMapper.selectByPrimaryKey(id);
        return article;
    }
//根据传入的id,进行点赞操作
    @Override
    public void thumbsUpById(Long id) {
        if(id==null||id<=0){
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article=articleMapper.selectByPrimaryKey(id);
//        帖子不存在
        if(article==null || article.getDeleteState()==1){
            log.warn(ResultCode.FAILED_ARTICLE_NOT_EXISTS.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_NOT_EXISTS));
        }
        if(article.getState()==1){
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED));
        }
        Article article1=new Article();
        article1.setId(article.getId());
        article1.setLikeCount(article.getLikeCount()+1);
        article1.setUpdateTime(new Date());
       int row=articleMapper.updateByPrimaryKeySelective(article1);
       if(row!=1){
           log.warn(ResultCode.ERROR_SERVICES.toString());
           throw new ApplicationException(AppResult.failed(ResultCode.ERROR_SERVICES));
       }
    }

    @Override
    public void deleteById(Long id) {
        //        还是要对Id,进行判断
        if(id<=0||id==null){
//            打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
//            抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
//根据id查询帖子信息
        Article article=articleMapper.selectByPrimaryKey(id);
        if(article==null||article.getDeleteState()==1){
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED));
        }
//       构造一个新的对象
        Article article1=new Article();
        article1.setId(article.getId());
        article1.setDeleteState((byte)1);
//        调用dao层进行更新
        int row=articleMapper.updateByPrimaryKeySelective(article1);
        if(row!=1){

            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));

        }
//        调用其他的两个操作
        iBoardService.subOneArticleCountById(article.getBoardId());
        iUserService.subOneArticleCountById(article.getUserId());

    }

    @Override
    public void addOneReplyCountById(Long id) {
//       首先还是对参数进行校验,传入的时贴子的id
        if(id<=0||id==null){
//            打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
//            抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
//        查询帖子,对帖子状态进行校验
        Article article=articleMapper.selectByPrimaryKey(id);
//        对帖子状态进行判断
        if(article==null||article.getDeleteState()==1){
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED));
        }
//        如果帖子已经封贴
        if(article.getState()==1){
            log.warn(ResultCode.FAILED_ARTICLE_BANNED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_ARTICLE_BANNED));
        }
//        构建帖子
        Article article1=new Article();
        article1.setId(article.getId());
//        更新回复数
        article1.setReplyCount(article.getReplyCount()+1);
        int row = articleMapper.updateByPrimaryKeySelective(article1);
        if(row!=1){
            log.warn(ResultCode.FAILED.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED));
        }


    }

    @Override
    public List<Article> selectByUserId(Long userId) {
//        首先还是对id,进行判断
        if(userId<=0||userId==null){
//            打印日志
            log.warn(ResultCode.FAILED_BOARD_ARTICLE_COUNT.toString());
//            抛出异常
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_BOARD_ARTICLE_COUNT));
        }
//        检验用户是否存在
        User user=iUserService.selectById(userId);
        if(user==null){
            log.warn(ResultCode.FAILED_CREATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_CREATE));
        }
//        调用接口返回结果
        List<Article> articles=articleMapper.selectByUserId(userId);
        return articles;
    }
}
